package com.lanchetech.job;


import com.lanchetech.service.QuartzService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class RoomStartNotifyJob extends QuartzJobBean {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private Scheduler scheduler;

    private QuartzService quartzService;

    @Autowired
    public void RoomStartNotifyJob(Scheduler scheduler, QuartzService quartzService) {
        this.scheduler = scheduler;
        this.quartzService = quartzService;
    }

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        Trigger trigger = jobExecutionContext.getTrigger();
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();

        // 将添加任务的时候存进去的数据拿出来
        String id = jobDataMap.getString("id");

        // TODO 编写任务的逻辑
        quartzService.roomStartNotify(Long.valueOf(id));

        // 执行之后删除任务
        try {
            // 暂停触发器的计时
            scheduler.pauseTrigger(trigger.getKey());
            // 移除触发器中的任务
            scheduler.unscheduleJob(trigger.getKey());
            // 删除任务
            scheduler.deleteJob(jobDetail.getKey());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
